package cn.iocoder.yudao.module.vote.service.voteuserinfodetail;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.vote.controller.admin.voteuserinfodetail.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserinfodetail.VoteUserInfoDetailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.vote.convert.voteuserinfodetail.VoteUserInfoDetailConvert;
import cn.iocoder.yudao.module.vote.dal.mysql.voteuserinfodetail.VoteUserInfoDetailMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.vote.enums.ErrorCodeConstants.*;

/**
 * 用户信息详情 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class VoteUserInfoDetailServiceImpl implements VoteUserInfoDetailService {

    @Resource
    private VoteUserInfoDetailMapper userInfoDetailMapper;

    @Override
    public Long createUserInfoDetail(VoteUserInfoDetailCreateReqVO createReqVO) {
        // 插入
        VoteUserInfoDetailDO userInfoDetail = VoteUserInfoDetailConvert.INSTANCE.convert(createReqVO);
        userInfoDetailMapper.insert(userInfoDetail);
        // 返回
        return userInfoDetail.getId();
    }

    @Override
    public void updateUserInfoDetail(VoteUserInfoDetailUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateUserInfoDetailExists(updateReqVO.getId());
        // 更新
        VoteUserInfoDetailDO updateObj = VoteUserInfoDetailConvert.INSTANCE.convert(updateReqVO);
        userInfoDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserInfoDetail(Long id) {
        // 校验存在
        this.validateUserInfoDetailExists(id);
        // 删除
        userInfoDetailMapper.deleteById(id);
    }

    private void validateUserInfoDetailExists(Long id) {
        if (userInfoDetailMapper.selectById(id) == null) {
            throw exception(USER_INFO_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public VoteUserInfoDetailDO getUserInfoDetail(Long id) {
        return userInfoDetailMapper.selectById(id);
    }

    @Override
    public VoteUserInfoDetailDO getDetailByUserId(Integer userId){
        return userInfoDetailMapper.selectByUserId(userId);
    }

    @Override
    public List<VoteUserInfoDetailDO> getUserInfoDetailList(Collection<Long> ids) {
        return userInfoDetailMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<VoteUserInfoDetailDO> getUserInfoDetailPage(VoteUserInfoDetailPageReqVO pageReqVO) {
        return userInfoDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VoteUserInfoDetailDO> getUserInfoDetailList(VoteUserInfoDetailExportReqVO exportReqVO) {
        return userInfoDetailMapper.selectList(exportReqVO);
    }

}
